#!/usr/bin/env python3
"""
面向 luarocks 软件的 MCP Server

MCP tools 列表：
1. luarocks_install - 安装 Lua 模块
   参数：package_name (str) - 要安装的包名
   返回：统一 JSON 结构

2. luarocks_list - 列出已安装的 Lua 模块
   参数：无
   返回：统一 JSON 结构

3. luarocks_show - 显示特定 Lua 模块的详细信息
   参数：package_name (str) - 要显示的包名
   返回：统一 JSON 结构

4. luarocks_version - 显示 luarocks 版本信息
   参数：无
   返回：统一 JSON 结构

5. luarocks_search - 搜索 Lua 模块
   参数：package_name (str) - 要搜索的包名
   返回：统一 JSON 结构

6. luarocks_help - 显示 luarocks 帮助信息
   参数：无
   返回：统一 JSON 结构

7. luarocks_path - 设置 Lua 路径环境变量
   参数：无
   返回：统一 JSON 结构
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json

mcp = FastMCP("Luarocks MCP Server")

@mcp.tool()
def luarocks_install(package_name: str, ctx: Context) -> dict:
    """
    安装 Lua 模块
    
    参数:
        package_name: 要安装的 Lua 模块名称，如 "luasocket"
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "install", package_name]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=300)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 300 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def luarocks_list(ctx: Context) -> dict:
    """
    列出已安装的 Lua 模块
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "list"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=60)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 60 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def luarocks_show(package_name: str, ctx: Context) -> dict:
    """
    显示特定 Lua 模块的详细信息
    
    参数:
        package_name: 要显示详细信息的 Lua 模块名称
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "show", package_name]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=60)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 60 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def luarocks_version(ctx: Context) -> dict:
    """
    显示 luarocks 版本信息
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "--version"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def luarocks_search(package_name: str, ctx: Context) -> dict:
    """
    搜索 Lua 模块
    
    参数:
        package_name: 要搜索的 Lua 模块名称
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "search", package_name]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=60)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 60 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def luarocks_help(ctx: Context) -> dict:
    """
    显示 luarocks 帮助信息
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "help"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def luarocks_path(ctx: Context) -> dict:
    """
    设置 Lua 路径环境变量
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["luarocks", "path", "--bin"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()